<?php
/**
 * Created by PhpStorm.
 * User: renshan
 * Date: 16-6-18
 * Time: 下午3:15
 */

namespace ApiBundle\Response;


/**
 * Class SecurityResponse
 * @package ApiBundle\Response
 * 统一封装安全访问类返回格式
 */
class SecurityResponse
{
    private $status;            //返回状态码
    private $message;           //返回信息
    private $data;              //返回数据

    /**
     * STATUS CODE
     */
    const SERVER_ERROR                  =   100;

    const APP_TOKEN_INVALID             =   110;
    const APP_TOKEN_INVALID_MESSAGE     =   "API只对APP端开放";
    
    /**
     * 登帐号密码登陆返回码
     */
    const EMPTY_USERNAME                =   104;
    const EMPTY_PASSWORD                =   103;
    const USERNAME_NOT_EXISTS           =   102;
    const PASSWORD_NOT_VALID            =   101;
    const USER_IS_VALID                 =   100;

    const EMPTY_USERNAME_MESSAGE        =   "帐号为空";
    const EMPTY_PASSWORD_MESSAGE        =   "密码为空";
    const USERNAME_NOT_EXISTS_MESSAGE   =   "帐号不存在";
    const PASSWORD_NOT_VALID_MESSAGE    =   "密码错误";
    const USER_IS_VALID_MESSAGE         =   "登录成功";

    /**
     * 通过api_token验证的返回码
     */
    const API_TOKEN_NOT_FOUND           =   209;
    const API_TOKEN_HAS_EXPIRED         =   208;
    const API_TOKEN_IS_VALID            =   200;
    const API_TOKEN_NOT_FOUND_MESSAGE   =   "帐号不存在";
    const API_TOKEN_HAS_EXPIRED_MESSAGE =   "api_token已过期";
    const API_TOKEN_IS_VALID_MESSAGE    =   "api_token验证成功";

    const TYPE_RETURN_OK                =   300;
    const TYPE_RETURN_ZERO              =   301;
    const TYPE_RETURN_OK_MESSAGE        =   "类型查找成功";
    const TYPE_RETURN_ZERO_MESSAGE      =   "服务端暂无类型";

    const CATEGORY_RETURN_OK            =   302;
    const CATEGORY_RETURN_ZERO          =   303;
    const CATEGORY_RETURN_OK_MESSAGE    =   "账单分类查找成功";
    const CATEGORY_RETURN_ZERO_MESSAGE  =   "暂无账单分类";

    const CATEGORY_CREATE_OK            =   304;
    const CATEGORY_HAS_EXISTS           =   305;
    const CATEGORY_CREATE_OK_MESSAGE    =   "创建分类成功";
    const CATEGORY_HAS_EXISTS_MESSAGE   =   "分类已存在";

    const CATEGORY_NOI_EXISTS           =   306;
    const CATEGORY_HAS_UPDATED          =   307;
    const CATEGORY_NOT_EXISTS_MESSAGE   =   "分类不存在或已被删除";
    const CATEGORY_HAS_UPDATED_MESSAGE  =   "分类已更新";

    const BILL_ADD_FAILURE              =   407;
    const BILL_ADD_SUCCESS              =   406;
    const BILL_ADD_FAILURE_MESSAGE      =   "账单记录失败";
    const BILL_ADD_SUCCESS_MESSAGE      =   "账单记录成功";

    /**
     * 只要通过了用户api_token验证，都有权限获取用户自己的账单
     * 所以不存在RETURN_FAILURE（系统错误由系统Exception接管）
     **/
    const BILL_RETURN_OK                =   408;
    const BILL_RETURN_OK_MESSAGE        =   "账单获取成功";


    public function __construct($status=0, $message="", array $data=[])
    {
        $this->status   = $status;
        $this->message  = $message;
        $this->data     = $data;
    }

    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function setMessage($msssage)
    {
        $this->message = $msssage;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    public function __toString()
    {
        return json_encode([
            'status'    => $this->status,
            'message'   => $this->message,
            'data'      => $this->data
        ]);
    }

    public function getResponseData()
    {
        return $this;
    }
}